#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/timer.h>

#define TIMER_TIMEOUT 5   // 5秒

static struct timer_list my_timer;

/* 定时器回调函数 */
static void timer_callback(struct timer_list *t)
{
    printk(KERN_INFO "Timer callback called (%ld).\n", jiffies);
    
    // 如果需要周期执行，重新激活
    mod_timer(&my_timer, jiffies + msecs_to_jiffies(5000));
}

static int __init timer_init(void)
{
    printk(KERN_INFO "Timer module loaded\n");

    /* 初始化定时器（推荐新接口） */
    timer_setup(&my_timer, timer_callback, 0);

    /* 启动定时器 */
    mod_timer(&my_timer, jiffies + msecs_to_jiffies(5000));

    return 0;
}

static void __exit timer_exit(void)
{
    printk(KERN_INFO "Timer module unloaded\n");

    /* 删除定时器，防止卸载时回调执行 */
    del_timer_sync(&my_timer);
}

module_init(timer_init);
module_exit(timer_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Linux Kernel Timer Example");